﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static LiWi.Shell;
namespace LiWi
{
    public partial class 报警处理 : Form
    {
        public WindowsAdapt adapt = new WindowsAdapt();
        IntPtr _handle { get { return Data.handle; } }
        bool openError = false;
        bool isConnect { get { return Data.handle == (IntPtr)0 ? false : true; } }
        bool setStatus = false;
        bool loopKey = false;
        int[] lastAlmCode = new int[5] { 0,0,0,0,0 };
        TextBox[] textBoxes = new TextBox[5];
        Thread loopThr = null;
        Dictionary<int,string> errCodeDesc = new Dictionary<int,string>();
        public 报警处理()
        {
            InitializeComponent();

            errCodeDesc.Add(1, "随动误差超限告警\n");
            errCodeDesc.Add(2, "与远程轴通讯出错\n");
            errCodeDesc.Add(3, "远程驱动器报错\n");
            errCodeDesc.Add(4, "正向硬限位\n");
            errCodeDesc.Add(5, "反向硬限位\n");
            errCodeDesc.Add(6, "找原点中\n");
            errCodeDesc.Add(8, "随动误差超限出错\n");
            errCodeDesc.Add(9, "超过正向软限位\n");
            errCodeDesc.Add(10, "超过负向软限位\n");
            errCodeDesc.Add(11, "CANCEL执行中\n");
            errCodeDesc.Add(12, "脉冲频率超过MAX_SPEED限制.需要修改降速或修改MAX_SPEED\n");
            errCodeDesc.Add(22, "告警信号输入\n");
            errCodeDesc.Add(23, "轴进入了暂停状态\n");



            textBoxes[0] = textBox_a0alm;
            textBoxes[1] = textBox_a1alm;
            textBoxes[2] = textBox_a2alm;
            textBoxes[3] = textBox_a3alm;
            textBoxes[4] = textBox_a4alm;


            adapt.SetForm(this);
        }
        ~报警处理() {
            loopKey = false;
          //  loopThr.Join();
            
        }

        private void loopTask() {
            while (loopKey) {
                if (openError) {
                    if (!setStatus) {
                        PMC_SetAlmIn(_handle, 0, Convert.ToInt32(comerror0.Text));     
                        PMC_SetAlmIn(_handle, 1, Convert.ToInt32(comerror1.Text));
                        PMC_SetAlmIn(_handle, 2, Convert.ToInt32(comerror2.Text));
                        PMC_SetAlmIn(_handle, 3, Convert.ToInt32(comerror3.Text));
                        PMC_SetAlmIn(_handle, 4, Convert.ToInt32(comerror4.Text));
                        PMC_SetInvertIn(_handle, Convert.ToInt32(comerror0.Text), Convert.ToInt32(checkBox_a0Invert.Checked));
                        PMC_SetInvertIn(_handle, Convert.ToInt32(comerror1.Text), Convert.ToInt32(checkBox_a1invert.Checked));
                        PMC_SetInvertIn(_handle, Convert.ToInt32(comerror2.Text), Convert.ToInt32(checkBox_a2invert.Checked));
                        PMC_SetInvertIn(_handle, Convert.ToInt32(comerror3.Text), Convert.ToInt32(checkBox_a3invert.Checked));
                        PMC_SetInvertIn(_handle, Convert.ToInt32(comerror4.Text), Convert.ToInt32(checkBox_a4invert.Checked));
                        setStatus = true;
                    }
                    for (int i = 0; i < 5; i++)
                    {
                        int errorNum=0;
                        PMC_GetAxisStatus(Data.handle, i, ref errorNum);
                        if (errorNum > 0 && errorNum != lastAlmCode[i])
                        {
                            
                            textBoxes[i].Clear();
                            for (int j = 1; j < 24; ++j)
                            {
                                if ((j > 0 && j < 6) || (j > 7 && j < 11) || j == 12 || j == 22)
                                {
                                    if (Data.GetBitValue(errorNum, (ushort)j))
                                    {   //有报警
                                        textBoxes[i].AppendText(string.Format("轴{0}:{1}",i ,errCodeDesc[j]));
                                        MessageBox.Show(textBoxes[i].Text);
                                    }
                                }
                            }
                            
                        }
                        else {
                            if (errorNum == 0) {
                                textBoxes[i].Clear();

                            }
                        }
                        lastAlmCode[i] = errorNum;
                    }
                }
                else {
                    setStatus = false;
                }   
                Thread.Sleep(100);
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void comerror2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comerror1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }



        private void comerror0_SelectedValueChanged(object sender, EventArgs e)
        {
            if (isConnect) {
                PMC_SetAlmIn(_handle, 0, Convert.ToInt32(comerror0.Text));
            }
        }

        private void comerror1_SelectedValueChanged(object sender, EventArgs e)
        {

            if (isConnect)
            {
                PMC_SetAlmIn(_handle, 1, Convert.ToInt32(comerror1.Text));
            }
        }

        private void comerror2_SelectedValueChanged(object sender, EventArgs e)
        {
            if (isConnect)
            {
                PMC_SetAlmIn(_handle, 2, Convert.ToInt32(comerror2.Text));
            }
        }

        private void comerror3_SelectedValueChanged(object sender, EventArgs e)
        {
            if (isConnect)
            {
                PMC_SetAlmIn(_handle, 3, Convert.ToInt32(comerror3.Text));
            }
        }

        private void comerror4_SelectedValueChanged(object sender, EventArgs e)
        {
            if (isConnect)
            {
                PMC_SetAlmIn(_handle, 4, Convert.ToInt32(comerror4.Text));
            }
        }

        private void checkBox_a0Invert_CheckedChanged(object sender, EventArgs e)
        {
            PMC_SetInvertIn(_handle, Convert.ToInt32(comerror0.Text), Convert.ToInt32(checkBox_a0Invert.Checked));
        }

        private void checkBox_a1invert_CheckedChanged(object sender, EventArgs e)
        {
            PMC_SetInvertIn(_handle, Convert.ToInt32(comerror1.Text), Convert.ToInt32(checkBox_a1invert.Checked));
        }

        private void checkBox_a2invert_CheckedChanged(object sender, EventArgs e)
        {
            PMC_SetInvertIn(_handle, Convert.ToInt32(comerror2.Text), Convert.ToInt32(checkBox_a2invert.Checked));
        }

        private void checkBox_a3invert_CheckedChanged(object sender, EventArgs e)
        {
            PMC_SetInvertIn(_handle, Convert.ToInt32(comerror3.Text), Convert.ToInt32(checkBox_a3invert.Checked));
        }

        private void checkBox_a4invert_CheckedChanged(object sender, EventArgs e)
        {
            PMC_SetInvertIn(_handle, Convert.ToInt32(comerror4.Text), Convert.ToInt32(checkBox_a4invert.Checked));
        }

        //开启报警
        private void btn_OpeError_Click(object sender, EventArgs e)
        {
            if (btn_OpeError.Text == "开启报警")
            {
                openError = true;
                loopKey = true;
                btn_OpeError.Text = "关闭报警";
                loopThr = new Thread(loopTask);
                loopThr.Start();
            }
            else
            {
                openError = false;
                loopKey = false;
                btn_OpeError.Text = "开启报警";

            }

        }
    }
}
